package class1.s2022350143028.service.Impl;

import class1.s2022350143028.data.GoodsData;
import class1.s2022350143028.data.OrdersData;
import class1.s2022350143028.model.OrdersModel;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.Scanner;

public class OrdersxyServiceImpl {
    OrdersData orderdata =new OrdersData();
    GoodsData goodsData=new GoodsData();
    Scanner sc = new Scanner(System.in);


    /**
     * 订单查询，多个条件可以继续加参数重载方法
     * @return boolean
     */
    //根据订单编号查询
    public boolean getList(){
        System.out.println("系统支持以下3种查询方式：1.订单编号 2.客户姓名 3.时间段查询");
        System.out.println("请选择你要查询的方式：");
        int choice= sc.nextInt();
        String query = null;
        switch (choice){
            case 1:{
                System.out.println("请输入要查询的订单编号：");
                query =sc.next();
                break;
            }
            case 2:{
                System.out.println("请输入要查询的客户姓名：");
                query =sc.next();
                break;
            }
            case 3:{
                System.out.println("请输入要查询的时间段：");
                query =sc.next();
                break;
            }
        }
        if(orderdata.Get(query,choice)) return true;
        else return false;

    }


    /**
     * 订单录入，注意新增订单时，对库存的更新
     * @return boolean
     */
    public boolean addOrders(){
        OrdersModel ordermodel = new OrdersModel ();
        System.out.println("开始录入订单：");

        //系统随机生成订单编号
        String head="S";
        Random rand =new Random();
        int number = rand.nextInt(9000) + 1000;
        String s=String.valueOf(number);
        String orderId=head+s;
        ordermodel.setOrderId(orderId);

        //获取当时时间
        LocalDateTime orderTime=LocalDateTime.now();
        ordermodel.setOrderTime(orderTime);

        System.out.println("请输入该订单的客户名称：");
        String name = sc.next();
        ordermodel.setClientName(name);

        System.out.println("请输入该订单的商品编号：");
        String goodId = sc.next();
        ordermodel.setGoodsId(goodId);

        System.out.println("请输入该订单的商品名称：");
        String goodname = sc.next();
        ordermodel.setGoodsName(goodname);

        System.out.println("请输入该订单的商品单价：");
        int price = sc.nextInt();
        ordermodel.setPrice(price);

        System.out.println("请输入该订单的商品数量：");
        int number1 = sc.nextInt();
        ordermodel.setQuantity(number1);

        if(orderdata.Save(ordermodel,goodsData.goodsDataList)){
            System.out.println("录入成功！");
            return true;
        }
        else {
            System.out.println("录入成功！");
            return false;
        }
    }

//
//    /**
//     * 订单编辑，注意编辑数量时，对库存的更新
//     * @return boolean
//     */
    public boolean editOrders(){
        OrdersModel ordermodel = new OrdersModel ();
        System.out.println("开始编辑订单：");

        System.out.println("请输入该订单的订单编号：");
        String orderid =sc.next();
        ordermodel.setOrderId(orderid);

//        System.out.println("请输入该订单的时间：");
//        String time=sc.next();
        LocalDateTime orderTime=LocalDateTime.now();
        //需要一个时间转化工具
        ordermodel.setOrderTime(orderTime);

        System.out.println("请输入该订单的客户名称：");
        String name = sc.next();
        ordermodel.setClientName(name);

        System.out.println("请输入该订单的商品编号：");
        String goodId = sc.next();
        ordermodel.setGoodsId(goodId);

        System.out.println("请输入该订单的商品名称：");
        String goodname = sc.next();
        ordermodel.setGoodsName(goodname);

        System.out.println("请输入该订单的商品单价：");
        int price = sc.nextInt();
        ordermodel.setPrice(price);

        System.out.println("请输入该订单的商品数量：");
        int number = sc.nextInt();
        ordermodel.setQuantity(number);

        if(orderdata.Update(ordermodel,goodsData.goodsDataList)){
            System.out.println("编辑成功！");
            return true;
        }
        else{
            System.out.println("编辑失败！");
            return false;
        }
    }


    /**
     * 订单删除，注意对库存的更新
     * @return boolean
     */
    public boolean delete(){
        System.out.println("请输入需要删除订单的订单编号：");
        String orderid =sc.next();
        if(orderdata.Delete(orderid,goodsData.goodsDataList)){
            System.out.println("删除成功！");
            return true;
        }
        else{
            System.out.println("删除失败！");
            return false;
        }
    }
}
